package vn.vfriends.vbox.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author tuancn
 */
@Entity
@Table(name="ACCOUNT")
@NamedQueries({
    @NamedQuery(name = "Account.findByUserId", query = "SELECT a FROM Account a WHERE a.user.id = :userId"),
    @NamedQuery(name = "Account.find", query = "SELECT a FROM Account a WHERE a.user.id = :userId AND a.name = :name")})
@XmlRootElement
public class Account implements Serializable {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Integer id;
    
    @Size(max=100)
    @Column(name = "OWN_ID",length=100, nullable=false)
    private String ownId;
    
    @Size(max=100)
    @Column(length=100, nullable=false)
    private String name;
    
    @Column(length=20, nullable=false)
    @Enumerated(EnumType.STRING)
    @NotNull
    private Provider provider;
    
    @Size(max = 200)
    @Column(name="ACCESS_TOKEN", length = 200)
    private String accessToken;
    
    @Size(max = 200)
    @Column(name="REFRESH_TOKEN", length = 200)
    private String refreshToken;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="REFRESH_DATE")
    private Date refreshDate;
    
    @Column(name="EXPIRES_IN")
    private long expiresIn;
    
    @ManyToOne
    @JoinColumn(name = "USER_ID")
    @NotNull
    private User user;

    @PrePersist
    public void prePersist() {
        if (refreshToken != null && refreshToken.length() > 0 && refreshDate == null) {
            refreshDate = new Date();
        }
    }
    
    @PreUpdate
    public void preUpdate() {
        if (refreshToken != null && refreshToken.length() > 0) {
            refreshDate = new Date();
        }
    }
    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOwnId() {
        return ownId;
    }

    public void setOwnId(String ownId) {
        this.ownId = ownId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Provider getProvider() {
        return provider;
    }

    public void setProvider(Provider provider) {
        this.provider = provider;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public Date getRefreshDate() {
        return refreshDate;
    }

    public void setRefreshDate(Date refreshDate) {
        this.refreshDate = refreshDate;
    }

    public long getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(long expiresIn) {
        this.expiresIn = expiresIn;
    }

    
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
